home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_font_win32.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.8 KB  |  179 lines

  1. //
  2. // "$Id: fl_font_win32.cxx,v 1.9 1999/01/07 19:17:39 mike Exp $"
  3. //
  4. // WIN32 font selection routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <config.h>
  27. #include <FL/Fl.H>
  28. #include <FL/fl_draw.H>
  29. #include <FL/win32.H>
  30. #include "Fl_Font.H"
  31.  
  32. #include <ctype.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. Fl_FontSize::Fl_FontSize(const char* name, int size) {
  37.   int weight = FW_NORMAL;
  38.   int italic = 0;
  39.   switch (*name++) {
  40.   case 'I': italic = 1; break;
  41.   case 'P': italic = 1;
  42.   case 'B': weight = FW_BOLD; break;
  43.   case ' ': break;
  44.   default: name--;
  45.   }
  46.   fid = CreateFont(
  47.     -size, // negative makes it use "char size"
  48.     0,                // logical average character width 
  49.     0,                // angle of escapement 
  50.     0,                // base-line orientation angle 
  51.     weight,
  52.     italic,
  53.     FALSE,            // underline attribute flag 
  54.     FALSE,            // strikeout attribute flag 
  55.     DEFAULT_CHARSET,    // character set identifier 
  56.     OUT_DEFAULT_PRECIS,    // output precision 
  57.     CLIP_DEFAULT_PRECIS,// clipping precision 
  58.     DEFAULT_QUALITY,    // output quality 
  59.     DEFAULT_PITCH,    // pitch and family 
  60.     name            // pointer to typeface name string 
  61.     );
  62.   if (!fl_gc) fl_GetDC(0);
  63.   SelectObject(fl_gc, fid);
  64.   GetTextMetrics(fl_gc, &metr);
  65. //  BOOL ret = GetCharWidthFloat(fl_gc, metr.tmFirstChar, metr.tmLastChar, font->width+metr.tmFirstChar);
  66. // ...would be the right call, but is not implemented into Window95! (WinNT?)
  67.   GetCharWidth(fl_gc, 0, 255, width);
  68. #if HAVE_GL
  69.   listbase = 0;
  70. #endif
  71.   minsize = maxsize = size;
  72. }
  73.  
  74. Fl_FontSize* fl_fontsize;
  75.  
  76. Fl_FontSize::~Fl_FontSize() {
  77. #if HAVE_GL
  78. // Delete list created by gl_draw().  This is not done by this code
  79. // as it will link in GL unnecessarily.  There should be some kind
  80. // of "free" routine pointer, or a subclass?
  81. // if (listbase) {
  82. //  int base = font->min_char_or_byte2;
  83. //  int size = font->max_char_or_byte2-base+1;
  84. //  int base = 0; int size = 256;
  85. //  glDeleteLists(listbase+base,size);
  86. // }
  87. #endif
  88.   if (this == fl_fontsize) fl_fontsize = 0;
  89.   DeleteObject(fid);
  90. }
  91.  
  92. ////////////////////////////////////////////////////////////////
  93.  
  94. // WARNING: if you add to this table, you must redefine FL_FREE_FONT
  95. // in Enumerations.H & recompile!!
  96. static Fl_Fontdesc built_in_table[] = {
  97. {" Arial"},
  98. {"BArial"},
  99. {"IArial"},
  100. {"PArial"},
  101. {" Courier New"},
  102. {"BCourier New"},
  103. {"ICourier New"},
  104. {"PCourier New"},
  105. {" Times New Roman"},
  106. {"BTimes New Roman"},
  107. {"ITimes New Roman"},
  108. {"PTimes New Roman"},
  109. {" Symbol"},
  110. {" Terminal"},
  111. {"BTerminal"},
  112. {" Wingdings"},
  113. };
  114.  
  115. Fl_Fontdesc* fl_fonts = built_in_table;
  116.  
  117. static Fl_FontSize* find(int fnum, int size) {
  118.   Fl_Fontdesc* s = fl_fonts+fnum;
  119.   if (!s->name) s = fl_fonts; // use 0 if fnum undefined
  120.   Fl_FontSize* f;
  121.   for (f = s->first; f; f = f->next)
  122.     if (f->minsize <= size && f->maxsize >= size) return f;
  123.   f = new Fl_FontSize(s->name, size);
  124.   f->next = s->first;
  125.   s->first = f;
  126.   return f;
  127. }
  128.  
  129. ////////////////////////////////////////////////////////////////
  130. // Public interface:
  131.  
  132. int fl_font_;
  133. int fl_size_;
  134. //static HDC font_gc;
  135.  
  136. void fl_font(int fnum, int size) {
  137.   if (fnum == fl_font_ && size == fl_size_) return;
  138.   fl_font_ = fnum; fl_size_ = size;
  139.   fl_fontsize = find(fnum, size);
  140. }
  141.  
  142. int fl_height() {
  143.   return (fl_fontsize->metr.tmAscent + fl_fontsize->metr.tmDescent);
  144. }
  145.  
  146. int fl_descent() {
  147.   return fl_fontsize->metr.tmDescent;
  148. }
  149.  
  150. double fl_width(const char* c) {
  151.   double w = 0.0;
  152.   while (*c) w += fl_fontsize->width[uchar(*c++)];
  153.   return w;
  154. }
  155.  
  156. double fl_width(const char* c, int n) {
  157.   double w = 0.0;
  158.   while (n--) w += fl_fontsize->width[uchar(*c++)];
  159.   return w;
  160. }
  161.  
  162. double fl_width(uchar c) {
  163.   return fl_fontsize->width[c];
  164. }
  165.  
  166. void fl_draw(const char* str, int n, int x, int y) {
  167.   SetTextColor(fl_gc, fl_RGB());
  168.   SelectObject(fl_gc, fl_fontsize->fid);
  169.   TextOut(fl_gc, x, y, str, n);
  170. }
  171.  
  172. void fl_draw(const char* str, int x, int y) {
  173.   fl_draw(str, strlen(str), x, y);
  174. }
  175.  
  176. //
  177. // End of "$Id: fl_font_win32.cxx,v 1.9 1999/01/07 19:17:39 mike Exp $".
  178. //
  179.